const express = require('express');
const router = express.Router();
const utils = require("../utils/index.js");
const {primary} = require("../utils/roleString");
const pools = require("../utils/pools.js");

// 获取售后列表
router.post('/list', async (req, res) => {
    try {
        const { customer_name, service_type } = req.body;

        // 构建SQL查询语句
        let sql = `SELECT * FROM aftersalesservice where 1=1`;

        if (customer_name) {
            sql += ' AND customer_name LIKE ' + '"%' + customer_name + '%"';
        }
        if (service_type) {
            sql += ' AND service_type LIKE ' + '"%' + service_type + '%"';
        }
        
        // 执行数据库查询
        await pools({sql,val:[],run:false,res,req});
        

    } catch (error) {
        // 返回错误响应
        res.json({
            code: 500,
            msg: '获取售后列表失败',
            error: error.message
        });
    }
});

// 添加售后记录
router.post('/add', async (req, res) => {
    try {
        // 获取请求体中的数据
        const { customer_name, service_type, service_record } = req.body;
        console.log(req.body)
        
        // 参数验证
        if (!customer_name || !service_type || !service_record) {
            return res.json({
                code: 400,
                msg: '请填写完整信息'
            });
        }
        
        // 构建插入SQL语句
        const sql = `INSERT INTO aftersalesservice 
                    (customer_name, service_type, service_record) 
                    VALUES (?, ?, ?)`;
        
        // 执行数据库插入操作
        await pools({sql, val:[customer_name, service_type, service_record],run:false,res,req});

    } catch (error) {
        // 返回错误响应
        res.json({
            code: 500,
            msg: '添加售后记录失败',
            error: error.message
        });
    }
});

// 更新售后记录
router.post('/update', async (req, res) => {
    try {
        // 获取请求体中的数据
        const { id, customer_name, service_type, service_record } = req.body;
        
        // 参数验证
        if (!id || !customer_name || !service_type || !service_record) {
            return res.json({
                code: 400,
                msg: '请填写完整信息'
            });
        }
        
        // 构建更新SQL语句
        const sql = `UPDATE aftersalesservice 
                    SET customer_name = ?, 
                        service_type = ?, 
                        service_record = ? 
                    WHERE id = ?`;
        
        // 执行数据库更新操作
        await pools({sql, val:[customer_name, service_type, service_record, id],run:false,res,req});
    } catch (error) {
        // 返回错误响应
        res.json({
            code: 500,
            msg: '更新售后记录失败',
            error: error.message
        });
    }
});

// 删除售后信息
router.post('/delete', async (req, res) => {
    try {
        // 获取要删除的记录ID
        const { id } = req.body;
        
        // 参数验证
        if (!id) {
            return res.json({
                code: 400,
                msg: '请提供要删除的记录ID'
            });
        }
        
        // 构建删除SQL语句
        const sql = `DELETE FROM aftersalesservice WHERE id = ?`;
        
        // 执行数据库删除操作
        await pools({sql, val:[id],run:false,res,req});

    } catch (error) {
        // 返回错误响应
        res.json({
            code: 500,
            msg: '删除售后记录失败',
            error: error.message
        });
    }
});

module.exports = router;
